/**
 * 
 */
package server;

import interfaces.AlarmServerInterface;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import server.alarmThreads.OnlineCheckerThread;
import server.config.ServerConfig;
import server.container.EinsatzAlarm;
import server.db.SQL;


/**
 * @author Christian
 * 
 */
public class AlarmServerMain extends UnicastRemoteObject implements AlarmServerInterface {
	
	/**
	 * 
	 */
	private static String version = "1.72";
	
	/**
	 * 
	 */
	private static AlarmServerMain rmi;
	
	/**
	 * 
	 */
	private ConcurrentLinkedQueue<EinsatzAlarm> list = new ConcurrentLinkedQueue<EinsatzAlarm>();
	
	/**
	 * 
	 */
	private static ServerConfig config = null;
	
	/**
	 * 
	 */
	private static Logger logger = Logger.getLogger(AlarmServerMain.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7091424932380147620L;
	
	/**
	 * 
	 * @throws RemoteException
	 */
	public AlarmServerMain() throws RemoteException {

		logger.debug("Konstructor AlarmAppRmiServerMain");
		Thread thread = new Thread(new ServerAlarmAppController(list));
		thread.start();
		OnlineCheckerThread.getInstance();
	}
	
	/**
	 * @see interfaces.AlarmServerInterface#addAlarm(java.lang.String)
	 */
	@Override
	public void addAlarm(String[] data) {

		logger.debug("AddAlarm");
		synchronized (list) {
			
			list.add(new EinsatzAlarm(data));
			list.notify();
		}
		
	}
	
	/**
	 * RMI-Server starten
	 */
	public static void main(String[] args) {

		PropertyConfigurator.configure("log4J.cfg");
		
		logger.info("Starte Server! Version: " + version);
		try {
			config = new ServerConfig("serverConfigLocal.cfg");
			logger.info("Config geladen");
		} catch (IOException e) {
			logger.error("Configfehler", e);
		}
		logger.info("DebugMode: " + ServerConfig.getInstance().isDebug());
		logger.info("Loggen: " + ServerConfig.getInstance().isLoggen());
		boolean sqlConnectionOk = false;
		while(!sqlConnectionOk) {
			try {
				SQL.getInstance();
				logger.info("SQL Verbindung steht");
				sqlConnectionOk = true;
			} catch (Exception e) {
				logger.error("MySQL Fehler " + e.getMessage(), e);
			}
			if(!sqlConnectionOk) {
				logger.info("Warte auf Serververbindung MYSQL");
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
				}
			}
		}
		try {
			System.setProperty("java.rmi.server.hostname", config.getRmiServerHostname());
			logger.info("Hostname: " + config.getRmiServerHostname());
			logger.info("Registerport: " + config.getRmiRegistryPort());
			LocateRegistry.createRegistry(config.getRmiRegistryPort());
			String chat_registry_name = "rmi://" + config.getRmiRegistryIp() + ":"
					+ config.getRmiRegistryPort() + "/Server";
			logger.debug(chat_registry_name);
			rmi = new AlarmServerMain();
			logger.info("Server an den Namen: " + chat_registry_name + " binden");
			Naming.rebind(chat_registry_name, rmi);
			logger.info("Server online!");
		} catch (MalformedURLException ex) {
			logger.error("URLException", ex);
		} catch (RemoteException ex) {
			logger.error("RemoteException", ex);
		}
	}

	/**
	 * @see interfaces.AlarmServerInterface#istServerOK()
	 */
	@Override
	public boolean istServerOK() {
		return OnlineCheckerThread.getInstance().istServerOK();
	}
}
